/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.PlcPressing;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.PlcPressingRepository;
import me.zhengjie.service.PlcPressingService;
import me.zhengjie.service.dto.PlcPressingDto;
import me.zhengjie.service.dto.PlcPressingQueryCriteria;
import me.zhengjie.service.mapper.PlcPressingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-15
**/
@Service
@RequiredArgsConstructor
public class PlcPressingServiceImpl implements PlcPressingService {

    private final PlcPressingRepository plcPressingRepository;
    private final PlcPressingMapper plcPressingMapper;

    @Override
    public PageResult<PlcPressingDto> queryAll(PlcPressingQueryCriteria criteria, Pageable pageable){
        Page<PlcPressing> page = plcPressingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(plcPressingMapper::toDto));
    }

    @Override
    public List<PlcPressingDto> queryAll(PlcPressingQueryCriteria criteria){
        return plcPressingMapper.toDto(plcPressingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PlcPressingDto findById(Integer id) {
        PlcPressing plcPressing = plcPressingRepository.findById(id).orElseGet(PlcPressing::new);
        ValidationUtil.isNull(plcPressing.getId(),"PlcPressing","id",id);
        return plcPressingMapper.toDto(plcPressing);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PlcPressing resources) {
        plcPressingRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PlcPressing resources) {
        PlcPressing plcPressing = plcPressingRepository.findById(resources.getId()).orElseGet(PlcPressing::new);
        ValidationUtil.isNull( plcPressing.getId(),"PlcPressing","id",resources.getId());
        plcPressing.copy(resources);
        plcPressingRepository.save(plcPressing);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            plcPressingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PlcPressingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PlcPressingDto plcPressing : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ip", plcPressing.getIp());
            map.put("内容", plcPressing.getContent());
            map.put("plc状态", plcPressing.getPlcStatus());
            map.put("有无载", plcPressing.getHasPallet());
            map.put("创建时间", plcPressing.getCreateTime());
            map.put("更新时间", plcPressing.getUpdateTime());
            map.put("port", plcPressing.getPort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}